package com.yonyou.gmmc.service.points.eventhandle;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.marketing.common.event.BizEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 创建生产处理类的工厂
 * 
 * @author BENJAMIN
 *
 */
public class EventHandleFactory {
	
	private static Logger logger = LoggerFactory.getLogger(EventHandleFactory.class);
	
	/**
	 * 生成对应的handle
	 * 
	 * @param event
	 * @return
	 */
	public static AbstractEventHandle createHandle(BizEvent event){
	    //直接读取配置文件来获取需要创建实例的类  
	    Properties p = new Properties();  
	    InputStream in = null;  
	    try {  
	        in = EventHandleFactory.class.getResourceAsStream("/event-handle.properties");  
	        p.load(in);  
	    } catch (IOException e) {  
	    	logger.error("装载工厂配置文件出错了，具体的堆栈信息如下：",e);  
	    }finally{  
	        try {  
	            if(in==null){
	                logger.info("配置文件为空");;  
	            }else{
	            in.close();
	            }
	        } catch (IOException e) {  
	        	logger.error("文件流关闭失败："+e);
	        	e.printStackTrace();
	        }  
	    }  
	    AbstractEventHandle handle = null;  
	    try {  
	    	handle = (AbstractEventHandle)Class.forName(p.getProperty(event.getEventBizStep().toString())).newInstance();  
	    } catch (InstantiationException e) {  
	    	logger.error("处理类初始化异常，具体的堆栈信息如下：",e);  
	    	return null;
	    } catch (IllegalAccessException e) {  
	    	logger.error("类权限异常，具体的堆栈信息如下：",e);  
	    	return null;
	    } catch (ClassNotFoundException e) {  
	    	logger.error("未找到对应的类，具体的堆栈信息如下：",e);  
	    	return null;
	    } catch(NullPointerException e) {
	    	logger.error("未找到对应的类，具体的堆栈信息如下：");
	    	return null;
	    }
	    return handle;  
	}  
}
